// components/lottery-product-edit/index.js
import componentBehavior from '../../../behaviors/component';
import { COMPONENT_OPTIONS, NUMERAL_ZH } from '../../../constants/common';
import { ACTIVITY_STATUS_VALUE } from '../../../constants/activity';
import { swapArr, deepClone } from '../../../common/utils';
import { tipToast } from '../../../common/env';

Component({
    behaviors: [componentBehavior],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        activityInfo: {
            type: Object,
            value: {},
        },
    },
    observers: {
        'activityInfo': function (val) {
            if (val) {
                const modifyFlag = !val.processStatus || val.processStatus === ACTIVITY_STATUS_VALUE.draft;
                this.setData({ modifyFlag });
            }
            if (val.activityProductWithSkus && val.activityProductWithSkus.length) {
                const cloneVal = deepClone(val);
                const activityProductWithSkus = cloneVal.activityProductWithSkus.map(ps => {
                    if (ps.skus.some(s => s.maximum <= 0)) {
                        ps.maximumTotal = '';
                    } else {
                        ps.maximumTotal = ps.skus.reduce((s, c) => s + parseFloat(c.maximum <= 0 ? 0 : c.maximum), 0);
                    }
                    return ps;
                });
                this.setData({ activityProductWithSkus });
            } else {
                this.setData({ activityProductWithSkus: [] });
            }
        },
    },
    data: {
        NUMERAL_ZH,
        activityProductWithSkus: [],
        modifyFlag: true,
    },
    methods: {
        _handleChange(data) {
            const { activityInfo, activityProductWithSkus } = this.data;
            activityInfo.activityProductWithSkus = activityProductWithSkus.map(ps => {
                delete ps.maximumTotal;
                return ps;
            });
            this.triggerEvent('change', {
                ...activityInfo,
                ...data,
            });
        },
        handleUp(e) {
            const { index } = e.currentTarget.dataset;
            let { activityProductWithSkus } = this.data;
            if (index === 0) return;
            activityProductWithSkus = swapArr(activityProductWithSkus, index, index - 1);
            this._handleChange({ activityProductWithSkus });
        },
        handleDown(e) {
            const { index } = e.currentTarget.dataset;
            let { activityProductWithSkus } = this.data;
            if (index === activityProductWithSkus.length - 1) return;
            activityProductWithSkus = swapArr(activityProductWithSkus, index, index + 1);
            this._handleChange({ activityProductWithSkus });
        },
        handleRemove(e) {
            if (!this.data.modifyFlag) {
                tipToast('活动已发布，不能删除商品');
                return;
            }
            const { index } = e.currentTarget.dataset;
            const { lottery1 } = this.data.activityInfo;
            let { activityProductWithSkus } = this.data;
            activityProductWithSkus.splice(index, 1);
            lottery1.prizeRates.splice(lottery1.prizeRates.length - 1, 1);
            this._handleChange({ activityProductWithSkus, lottery1 });
        },
        handleModify(e) {
            const { productId } = e.currentTarget.dataset;
            this.triggerEvent('modify', productId);
        },
        handleAdd(e) {
            if (!this.data.modifyFlag) {
                tipToast('活动已发布，不能添加商品');
                return;
            }
            this.triggerEvent('add');
        },
    }
})
